package app.im;

import android.content.Intent;
import android.support.annotation.NonNull;

import com.lidroid.xutils.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import app.AddressBookDeptSelector;
import application.AppApplication;
import application.HttpUrlConstant;
import interfaces.IReturnObject;
import model.EmployeeItemDBModel;

/**
 * Created by ThinkPad on 2017/5/15.
 */

public class IMAddChatPersenter {

    private IMAddChat activity;
    private IMAddChatModel model;
    public IMAddChatPersenter(IMAddChat activity){
        this.activity = activity;
        this.model = new IMAddChatModel();
    }
    public void getFirstListData(){
        getListData(model.deptID);
    }
    public void getListData(final String deptID) {
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("__session__", AppApplication.getSession());
        params.addQueryStringParameter("deptid", deptID);
        activity.httpGet(HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.AccountList), params, new IReturnObject<String>() {
            @Override
            public void returnResult(String result) {
                try {
                    JSONObject jsonResult = new JSONObject(result);
                    if (jsonResult.getBoolean("Succeed")) {
                        List<EmployeeItemDBModel> listData = model.getEmployeeItemDBModels(jsonResult);
                        if (listData.size() > 0) {
                            activity.dataNotifyDataSetChanged(listData);
                            activity.showContent();
                        } else {
                            activity.showEmpty();
                        }
                        model.setDeptID(deptID);
                    } else {
                        activity.showError(new IReturnObject<String>() {
                            @Override
                            public void returnResult(String result) {
                                getListData(deptID);
                            }
                        });
                    }
                } catch (JSONException e) {
                    activity.showError(new IReturnObject<String>() {
                        @Override
                        public void returnResult(String result) {
                            getListData(deptID);
                        }
                    });
                }
            }
        });
    }

    public void getIntentData(Intent intent) {
        model.deptID = intent.getStringExtra("deptID");
        model.title  = intent.getStringExtra("title");
    }


}
